﻿using System;
using System.ComponentModel;
using MECPSDB.EntityClasses;
using MECPSDB.FactoryClasses;
using MECPSDB.HelperClasses;
using MECUITMPLTLIB;
using MECPSDB.DatabaseSpecific;
using SD.LLBLGen.Pro.ORMSupportClasses;

namespace MEC.WMS.FileMaintenance.Material.MatImg {
    
    public partial class AeFrm:AeFrmUiTemplate{
        
        private const string Caption = "Material Image Preview";
        
        public AeFrm(string cs, string branchcode, string usercode){
            if (cs == null){
                throw new ArgumentNullException("cs");
            }
            Cs= cs;
            Branchcode = branchcode;
            Usercode = usercode;
            InitializeComponent();
            this.Text = Caption;
        }

        public override sealed string Text{
            get{
                return base.Text;
            }
            set{
                base.Text = value;
            }
        }

        public override void MethodNewEntityProcedure(){
           base.MethodNewEntityProcedure();
           //todo set default for the newly added entity by using another object
           MaterialEntity materialEntity = new MaterialEntity(); //-implementation
           //todo Add extra code here
           //... 
           //todo assign the new entity to the entity object bounded to the form 
           MainEntity = materialEntity; //-implementation
           //todo must put at the end when there is no error - allow edit to true
           MethodAllowEdit(true); ////-implementation
       }

        protected override void AeFrmUiTemplateLoad(object sender, EventArgs e){
        //implements generic setting of Dexex controls behavior
            base.AeFrmUiTemplateLoad(sender,e); 
        //todo
        }

        protected override void MainEntityBackgroundSaveWorkCompleted(object sender, RunWorkerCompletedEventArgs e){
            base.MainEntityBackgroundSaveWorkCompleted(sender,e);
        //todo
        }

        protected override void MainEntityBackgroundSaveDoWork(object sender, DoWorkEventArgs e){
            MatimgEntity entity = (MatimgEntity) e.Argument;
            using (DataAccessAdapter dataAccessAdapter = new DataAccessAdapter(Cs)){

                if (entity.IsNew){} else{}

                dataAccessAdapter.SaveEntity(entity, true);
                e.Result = entity;
            }

        }

        protected override void MethodLoadControlValuesToObjectsForSaving(){
            MatimgEntity entity = (MatimgEntity) MainEntity;
            entity.MatImg = (byte[]) pictureEditImage_ae_.EditValue;
       }

        protected override bool MethodValidate(){
            dxErrorProvider1.ClearErrors();
            bool focused = false;

            return !dxErrorProvider1.HasErrors;
        }

        protected override void MethodClearForm(){
        //todo
            base.MethodClearForm();
            //todo extra handling
            pictureEditImage_ae_.Image = null;
            //checkEditInActive_ae_.Checked = false;
            //checkEditWdSerial_ae_.Checked = false;
        }

        protected override void MethodDispayMainEntityToControls(){
        //todo
            base.MethodDispayMainEntityToControls();
            MatimgEntity entity = (MatimgEntity) MainEntity;
            if (entity.MatImg != null) pictureEditImage_ae_.EditValue = entity.MatImg;
            //MaterialEntity material = (MaterialEntity) MainEntity;
            //TextEditMaterialCode_ae_pk_.EditValue = material.MaterialCode;
            //TextEditDescription_ae_.EditValue = material.MatDescription;

            //lookUpEditBrandCode_ae_.EditValue = material.BrandCodeFk;
            //lookUpEditGroupCode_ae_.EditValue = material.MatGrpCodeFk;
            //lookUpEditUomCode_ae_.EditValue = material.UmCodeFk;

            //spinEditCritical_ae_.EditValue = material.CritLevel;
            //spinEditInvLevel_ae_.EditValue = material.InvLevel;
            //spinEditReorder_ae_.EditValue = material.ReOrderLevel;

            //checkEditInActive_ae_.EditValue = material.InActive;
            //checkEditWdSerial_ae_.EditValue = material.InActive;

            //memoEditRemarks_ae_.EditValue = material.Remarks;
        }

        protected override void MainEntityBackgroundFetchDoWork(object sender, DoWorkEventArgs e){
            FetchParam fetchParam = (FetchParam) e.Argument;
           
            //TODO MainEntityFetch
            if (fetchParam.FetchMainEntity){
                MatimgEntity entity = new MatimgEntity(fetchParam.PkCode);
                IPrefetchPath2 prefetchPath = null;
                ExcludeIncludeFieldsList excludedfields = new ExcludeIncludeFieldsList();
                using (DataAccessAdapter dataAccessAdapter = new DataAccessAdapter(fetchParam.Cs)){
                    dataAccessAdapter.FetchEntity(entity, prefetchPath, null, excludedfields);
                    fetchParam.MainEntityFetched = entity;
                    e.Result = fetchParam;
                    return;
                }
            }
        }


        
    }//End class
} // End namespace